package com.dmlll.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmlll.VO.HospitalPurchaseInputVO;
import com.dmlll.common.Constant;
import com.dmlll.common.DataGridView;
import com.dmlll.common.ResultObj;
import com.dmlll.domain.*;
import com.dmlll.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 采购单药品明细 前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-03-31
 */
@RestController
public class PurchaseDrupDetailController {
    @Autowired
    private PurchaseDrupDetailService purchaseDrupDetailService;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private DrugMessageService drugMessageService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private HospitalPurchaseInputService hospitalPurchaseInputService;

    /**
     * 在purchase_drup_detail更新供应商id
     * 更新交易价、数量、总金额
     *
     * @param purchaseDrupDetail
     * @return
     */
    @RequestMapping("/updateProviderId")
    public ResultObj updateProviderId(PurchaseDrupDetail purchaseDrupDetail) {
        try {
            purchaseDrupDetailService.updateById(purchaseDrupDetail);
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;
        }
        return ResultObj.UPDATE_SUCCESS;
    }

    /**
     * 批量修改
     *
     * @param purchaseDrupDetailStr
     * @return
     */
    @RequestMapping("/updatePurchaseDrupDetails")
    public ResultObj updatePurchaseDrupDetails(String purchaseDrupDetailStr) {
        List<PurchaseDrupDetail> detailList = JSONArray.parseArray(purchaseDrupDetailStr, PurchaseDrupDetail.class);
        try {
            purchaseDrupDetailService.updateBatchById(detailList);
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;
        }
        return ResultObj.UPDATE_SUCCESS;
    }

    /**
     * 添加采购药品
     *
     * @param drugMessageStr 药品信息
     * @param providerId     供应商Id
     * @param purchaseId     采购单Id
     * @return
     */
    @RequestMapping("/addPurchaseDrupDetail")
    public ResultObj addPurchaseDrupDetail(String drugMessageStr, Integer[] providerId, Integer purchaseId) {
        List<DrugMessage> drugMessages = JSON.parseArray(drugMessageStr, DrugMessage.class);
        List<PurchaseDrupDetail> detailList = new ArrayList<>();
        for (int i = 0; i < drugMessages.size(); i++) {
            PurchaseDrupDetail drupDetail = new PurchaseDrupDetail();
            drupDetail.setDrugMessageId(drugMessages.get(i).getId());
            drupDetail.setProviderId(providerId[i]);
            drupDetail.setPurchaseId(purchaseId);
            drupDetail.setWinPrice(drugMessages.get(i).getWinPrice());
            drupDetail.setPurchaseState(Constant.PURCHASE_STATE_ONE);
            detailList.add(drupDetail);
        }
        try {
            purchaseDrupDetailService.saveBatch(detailList);
        } catch (Exception e) {
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }

    @RequestMapping("/deletePurchaseDrupDetail")
    public ResultObj deletePurchaseDrupDetail(Integer[] purchaseDrupDetailIds) {
        try {
            purchaseDrupDetailService.removeByIds(Arrays.asList(purchaseDrupDetailIds));
        } catch (Exception e) {
            return ResultObj.DELETE_ERROR;
        }
        return ResultObj.DELETE_SUCCESS;
    }

    /**
     * @param purchaseDrupDetail
     * @param purchase
     * @param provider
     * @param drugMessage
     * @param input
     * @return
     */
    @RequestMapping("getInputPurchaseDrupDetail")
    public DataGridView getInputPurchaseDrupDetail(PurchaseDrupDetail purchaseDrupDetail, Purchase purchase, Provider provider, DrugMessage drugMessage, HospitalPurchaseInputVO input) {
        IPage<PurchaseDrupDetail> iPage = new Page<>(input.getPage(),input.getLimit());
        //查询条件进行判断
        IPage<PurchaseDrupDetail> page = purchaseDrupDetailService.getAvaPurchaseDrupDetail(iPage,purchaseDrupDetail, purchase, drugMessage, purchaseDrupDetail.getProviderId(), input);
        for (PurchaseDrupDetail pdd : page.getRecords()) {
            pdd.setPurchase(purchaseService.getById(pdd.getPurchaseId()));
            pdd.setProvider(providerService.getById(pdd.getProviderId()));
            pdd.setDrugMessage(drugMessageService.getById(pdd.getDrugMessageId()));
            QueryWrapper<HospitalPurchaseInput> qw = new QueryWrapper<>();
            qw.eq("purchase_id",pdd.getPurchaseId());
            qw.eq("drug_message_id",pdd.getDrugMessageId());
            HospitalPurchaseInput hospitalPurchaseInput = hospitalPurchaseInputService.getOne(qw);
            if(hospitalPurchaseInput==null) {
                hospitalPurchaseInput = new HospitalPurchaseInput();
                hospitalPurchaseInput.setPurchaseId(pdd.getPurchaseId());
                hospitalPurchaseInput.setDrugMessageId(pdd.getDrugMessageId());
            }
            pdd.setInput(hospitalPurchaseInput);
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }
    @RequestMapping("getDrugMessageByPurchaseId")
    public Collection<DrugMessage> getDrugMessageByPurchaseId(Integer purchaseId){
        //获得所有purchaseId的采购细节
        QueryWrapper<PurchaseDrupDetail> qw = new QueryWrapper<>();
        qw.eq("purchase_id",purchaseId);
        List<PurchaseDrupDetail> drugCatalogControls = purchaseDrupDetailService.list(qw);
        List<Integer> drugMessages = new ArrayList<>();
        Collection<DrugMessage> messages = null;
        for (PurchaseDrupDetail control:drugCatalogControls){
            drugMessages.add(control.getDrugMessageId());
        }
        if(drugMessages.size()>0) {
            messages = drugMessageService.listByIds(drugMessages);
        }
        return messages;
    }
}


